<?php
use think\config;
/**
 * Created by PhpStorm.
 * User: wcy
 * Date: 17-8-16
 * Time: 下午4:39
 */
class qqSDK{
    const VERSION = "2.0";
    const GET_AUTH_CODE_URL = "https://graph.qq.com/oauth2.0/authorize";
    const GET_ACCESS_TOKEN_URL = "https://graph.qq.com/oauth2.0/token";
    const GET_OPENID_URL = "https://graph.qq.com/oauth2.0/me";
    const GET_USER_INFO ="https://graph.qq.com/user/get_user_info";
    //QQ登录
    public function qq_login(){
        $appid = config::get("sdk_config.qq_login")["APP_ID"];
        $callback = config::get("sdk_config.qq_login")["CALLBACK"];
        //-------生成唯一随机串防CSRF攻击
        $state = md5(uniqid(rand(), TRUE));
        //-------构造请求参数列表
        $keysArr = array(
            "response_type" => "code",
            "client_id" => $appid,
            "redirect_uri" => $callback,
            "state" => $state,
        );
        $login_url =  $this->combineURL(self::GET_AUTH_CODE_URL, $keysArr);
        return $login_url;
    }
    //获取token
    public function qq_token($code){
        $appid = config::get("sdk_config.qq_login")["APP_ID"];
        $appkey = config::get("sdk_config.qq_login")["APP_Key"];
        $callback = config::get("sdk_config.qq_login")["CALLBACK"];
        //-------构造请求参数列表
        $keysArr = array(
            "grant_type" => "authorization_code",
            "client_id" => $appid,
            "client_secret" => $appkey,
            "redirect_uri" => $callback,
            "code" => $code,
        );
        //------构造请求access_token的url
        $token_url =  $this->combineURL(self::GET_ACCESS_TOKEN_URL, $keysArr);
        $response = $this->get_contents($token_url);
        if(strpos($response, "callback") !== false){
            $lpos = strpos($response, "(");
            $rpos = strrpos($response, ")");
            $response  = substr($response, $lpos + 1, $rpos - $lpos -1);
            $msg = json_decode($response);
        }
        $params = array();
        parse_str($response, $params);
        return $params["access_token"];
    }
    //获取openid
    public function qq_openid($access_token){
        //-------构造请求参数列表
        $keysArr = array(
            "access_token" => $access_token,
        );
        //------构造请求access_token的url
        $graph_url=  $this->combineURL(self::GET_OPENID_URL, $keysArr);
        $response = $this->get_contents($graph_url);
        //--------检测错误是否发生
        if(strpos($response, "callback") !== false){
            $lpos = strpos($response, "(");
            $rpos = strrpos($response, ")");
            $response = substr($response, $lpos + 1, $rpos - $lpos -1);
        }
        $user = json_decode($response);
        return $user->openid;
    }
    //获取用户信息
    public function get_user_info($access_token,$openid){
        //-------构造请求参数列表
        $keysArr = array(
            "access_token" => $access_token,
            "oauth_consumer_key" => config::get("sdk_config.qq_login")["APP_ID"],
            "openid" => $openid,
        );
        //------构造请求access_token的url
        $graph_url=  $this->combineURL(self::GET_USER_INFO, $keysArr);
        $response = $this->get_contents($graph_url);
        $user = json_decode($response);
        return $user;
    }
    /**
     * combineURL
     * 拼接url
     * @param string $baseURL   基于的url
     * @param array  $keysArr   参数列表数组
     * @return string           返回拼接的url
     */
    public  function combineURL($baseURL,$keysArr){
        $combined = $baseURL."?";
        $valueArr = array();
        foreach($keysArr as $key => $val){
            $valueArr[] = "$key=$val";
        }
        $keyStr = implode("&",$valueArr);
        $combined .= ($keyStr);
        return $combined;
    }
    /**
     * get_contents
     * 服务器通过get请求获得内容
     * @param string $url       请求的url,拼接后的
     * @return string           请求返回的内容
     */
    public static   function get_contents($url){
        if (ini_get("allow_url_fopen") == "1") {
            $response = file_get_contents($url);
        }else{
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_URL, $url);
            $response =  curl_exec($ch);
            curl_close($ch);
        }
        return $response;
    }
}